package com.gangwangchina.android.readsms.service;

import com.gangwangchina.android.readsms.config.Global;
import com.gfan.sdk.statistics.Collector;
import com.shoushuo.android.tts.ITts;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.gsm.SmsMessage;

public class readServer extends Service {

	private ITts ttsService;

	private boolean ttsBound;

	private Thread m_WatchPhoneIsDwonThread;

	private float x, y, z;

	private ServiceConnection connection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName className, IBinder iservice) {
			ttsService = ITts.Stub.asInterface(iservice);
			Global.Instance().setItts(ttsService);
			ttsBound = true;
			// 在应用第一个使用TTS的地方，调用下面的initialize方法，比如如果有
			// 两个Activity都使用手说TTS，则第二个Activity在此不需要再调用。
			try {
				ttsService.initialize();
			} catch (RemoteException e) {
			}

		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			ttsService = null;
			ttsBound = false;
		}
	};

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();

		Global.Instance().Init(this);

		SensorManager sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
		Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		SensorEventListener lsn = new SensorEventListener() {
			public void onSensorChanged(SensorEvent e) {
				x = e.values[SensorManager.DATA_X];
				y = e.values[SensorManager.DATA_Y];
				z = e.values[SensorManager.DATA_Z];

				if (((int) z <= -2) && Global.Instance().config.bDownstopread
						&& (Global.Instance().ACTION_STOP_READ_SENDED == false)) {
//					Global.Instance()
//							.getContext()
//							.sendBroadcast(
//									new Intent(
//											Global.Instance().ACTION_STOP_READ));
//					Global.Instance().ACTION_STOP_READ_SENDED = true;
					
					
					if( Global.Instance().isSpeaking() )
					{
						Global.Instance().stopSpeaking();
					}
				}
			}

			public void onAccuracyChanged(Sensor s, int accuracy) {
			}
		};
		// 注册listener，第三个参数是检测的精确度
		sensorMgr
				.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
		
		Collector.setAppClickCount("Start_i_read_Server");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		if (!ttsBound) {
			String actionName = "com.shoushuo.android.tts.intent.action.InvokeTts";
			Intent newintent = new Intent(actionName);
			this.bindService(newintent, connection, Context.BIND_AUTO_CREATE);
		}

	}

	@Override
	public void onDestroy() {
		if (ttsBound) {
			ttsBound = false;
			this.unbindService(connection);
		}
		super.onDestroy();
	}

}
